/*-
 * Copyright (c) 2006 Vishal Patil <bsd.devil@gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */

#ifndef _KERNEL_
#include <stdlib.h>
#endif

#include <string.h>
#include "iscsi_util.h"
#include "iscsi_login_params.h"

#define set_int_param(param,value) {\
	int val = atoi(value);\
	param = val; \
}

#define test_and_set_int_param(param,value) {\
	int val = atoi(value);\
	if (val > param) {\
		return -1; \
	} else { \
		param = val; \
	}\
}

#define set_boolean_param(param,value) {\
	if (strcmp(value,"Yes") == 0) {\
		param = 1;\
	} else {\
		param = 0;\
	}\
}

int 
set_session_params(struct iscsi_session * sess,
		struct iscsi_pdu_incore * pdu) {

	char value[64];	

	if (get_key_value("MaxRecvDataSegmentLength",pdu,value)) {
		test_and_set_int_param(
			sess->login_params->maxRecvDataSegmentLength,
			value);
	}

	if (get_key_value("MaxXmitDataSegmentLength",pdu,value)) {
		test_and_set_int_param(
			sess->login_params->maxRecvDataSegmentLength,
			value);
	}

	if (get_key_value("MaxBurstLength",pdu,value)) {
		test_and_set_int_param(sess->login_params->maxBurstLength,
				value);
	}

	if (get_key_value("FirstBurstLength",pdu,value)) {
		test_and_set_int_param(
				sess->login_params->firstBurstLength,
				value);
	}
	
	if (get_key_value("DefaultTime2Wait",pdu,value)) {
		set_int_param(sess->login_params->defaultTime2Wait,
				value);
	}

	if (get_key_value("DefaultTime2Retain",pdu,value)) {
		set_int_param(sess->login_params->defaultTime2Retain,
				value);
	}

	if (get_key_value("MaxOutstandingR2T",pdu,value)) {
		set_int_param(sess->login_params->maxOutstandingR2T,
				value);
	}

	if (get_key_value("ErrorRecoveryLevel",pdu,value)) {
		set_int_param(sess->login_params->errorRecoveryLevel,
				value);
	}

	if (get_key_value("TargetPortalGroupTag",pdu,value)) {
		set_int_param(sess->login_params->targetPortalGroupTag,
				value);
	}

	if (get_key_value("InitialR2T",pdu,value)) {
		set_boolean_param(sess->login_params->initialR2T,
				value);
	}

	if (get_key_value("ImmediateData",pdu,value)) {
		set_boolean_param(sess->login_params->immediateData,
				value);
	}
	
	if (get_key_value("DataPDUInOrder",pdu,value)) {
		set_boolean_param(sess->login_params->dataPDUInOrder,
				value);
	}
	
	if (get_key_value("DataSequenceInOrder",pdu,value)) {
		set_boolean_param(sess->login_params->dataSequenceInOrder,
				value);
	}
	return 0;
}

